<?php
/**
 * Admin_Libraries_Controller_Action
 * @version            $Id: Action.php 01-08-2010
 * @file            Action.php
 */
abstract class Admin_Libraries_Controller_Action extends Zend_Controller_Action
{
	protected $_auth = null;
	protected $_acl = null;
    
    public function init()
    {
        $config = Zend_Registry::get ( 'configuration' );               
        Zend_Layout::getMvcInstance()->setLayoutPath(APP_PATH.DS."modules".DS."admin".DS.'layouts')
        							 ->setLayout('default');
        // Load meta tags
        $this->view->headMeta ()->appendHttpEquiv ( 'Content-Type', 'text/html;charset=' . $config->charset );
        $this->view->headMeta ()->appendHttpEquiv ( 'Content-Language', 'en-US' );
        $this->view->headMeta ()->appendName ( 'keywords', $config->meta->keywords );
        $this->view->headMeta ()->appendName ( 'author', $config->meta->author );
        $this->view->headMeta ()->appendName ( 'description', $config->meta->description );
        
        // Set doctype and charset
        $this->view->setEncoding ( 'UTF-8' );
        $this->view->doctype ( 'XHTML1_STRICT' );
        
        // Setting up title
        $this->view->headTitle ()->setSeparator ( ' - ' ); // setting a separator string for segments:
        $this->view->headTitle ( 'Demo site' );
        
        // Load file
        $this->view->headLink ()->appendStylesheet ( $this->view->baseUrl () . '/html/admin/css/template.css' );
        $this->view->headLink ()->appendStylesheet ( $this->view->baseUrl () . '/html/admin/css/system.css' );
        $this->view->headScript ()->appendFile ( $this->view->baseUrl () . '/html/js/jlib.js', 'text/javascript');
        $this->loadBlock();              
        $objAuth = new Core_Auth('admin');   
        if ($objAuth->isLogged()) {
        	$this->_auth = $objAuth->getIdentity();   
        	$this->view->userProfile = $this->_auth;
        	$this->_acl = new Core_Acl($this->_auth->id);   
        } else {
        	if ($this->getRequest()->getActionName() != 'login') {        		
        		if (($returnUrl = $this->getRequest()->getRequestUri()) != '')
        			$returnUrl = '/return/' . base64_encode($returnUrl);
        		$this->_redirect('/admin/auth/login' . $returnUrl);	
        	}
        }       
    }   
    
    /**
     * @todo load all block
     * @return void
     */
    private function loadBlock() {
    	$blocksConfig = new Zend_Config_Ini ( APP_PATH .DS . 'modules' . DS . 'admin' . DS . 'configs' . DS . 'blocks.ini' );
    	$blocks = $blocksConfig->toArray ();
    	    	
    	foreach ( $blocks as $blockName => $options ) {
    		if ($options ['display'] == 'all') {
    			$this->_helper->block->add ( $blockName );
    		} elseif ($options ['display'] != 'none') {
    			$modules = explode ( ',', $options ['display'] );
    			for($i = 0; $i < count ( $modules ); $i ++) {
    				if ($this->_request->getModuleName () == $modules [$i]) {
    					$this->_helper->block->add ( $blockName );
    					break;
    				}
    			}
    		}
    	}
    }
    
    protected function showMessage($message) {
    	return $this->view->partial('partial/message.phtml', array('message'=>$message));
    }
}
